﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using Telerik.Web.Mvc;
using ThuySan.Bussiness;
using ThuySan.Models;

#endregion

namespace ThuySan.Areas.Backend.Controllers
{
    [Authorize]
    public class UserController : Controller
    {
        private readonly UnitOfWork _unitOfWork = new UnitOfWork();

        public ActionResult Index()
        {
            return View();
        }

        [GridAction]
        public ActionResult GetList()
        {
            return View(new GridModel(_unitOfWork.UserRepository.Select().Select(c => new {
                                                                                                  c.WebUserId,
                                                                                                  c.UserName,
                                                                                                  c.Email
                                                                                          })));
        }

        public ActionResult Add()
        {
            return View(new WebUserModel());
        }

        [ValidateInput(false), HttpPost]
        public ActionResult Add(WebUserModel webUserModel)
        {
            if (string.IsNullOrEmpty(webUserModel.Password))
                ModelState.AddModelError("Password", "Value is required");
            if (ModelState.IsValid)
            {
                WebUser webUser = webUserModel;
                if (_unitOfWork.UserRepository.CheckInsertWebUser(webUser))
                {
                    Exception exception;
                    _unitOfWork.UserRepository.Insert(webUser, out exception);
                    if (exception == null)
                    {
                        return RedirectToAction("Index");
                    }
                }
                else
                {
                    ModelState.AddModelError("error", "Username or Email has exists. Please try again");
                }
            }
            return View(webUserModel);
        }

        public ActionResult Edit(string id)
        {
            int guid;
            if (int.TryParse(id, out guid))
            {
                WebUser webUser = _unitOfWork.UserRepository.SelectbyId(guid);
                if (webUser != null)
                {
                    WebUserModel webUserModel = webUser;
                    return View(webUserModel);
                }
            }
            return RedirectToAction("Index");
        }

        [ValidateInput(false), HttpPost]
        public ActionResult Edit(WebUserModel webUserModel)
        {
            if (ModelState.IsValid)
            {
                WebUser webUser = _unitOfWork.UserRepository.SelectbyId(webUserModel.WebUserId);
                webUserModel.Update(webUser);
                if (_unitOfWork.UserRepository.CheckUpdateWebUser(webUser))
                {
                    Exception exception;
                    _unitOfWork.UserRepository.Update(webUser, out exception);

                    return RedirectToAction("Index");
                }
                ModelState.AddModelError("error", "Username or Email has exists. Please try again");
            }
            return View(webUserModel);
        }

        [HttpPost]
        public ActionResult Delete(string id)
        {
            if (id != null)
            {
                string[] s = id.Split(',');
                int guid;
                List<int> list = (from s1 in s where int.TryParse(s1, out guid) select int.Parse(s1)).ToList();

                if (_unitOfWork.UserRepository.CheckDeleteWebUser(list))
                {
                    _unitOfWork.UserRepository.Delete(list);
                }
            }
            return Json(new {
                                    err = 0,
                                    module = "Static Content",
                                    msg = "Update Successfully"
                            }, JsonRequestBehavior.DenyGet);
        }

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            return Redirect("/");
        }
    }
}
